package main

import (
	"fmt"
	"io"
	"os"
	"strings"
)

// 复制图片
func copyImg(src string, dst string) {
	// 写
	fWrite, err := os.Create(dst)
	if err != nil {
		fmt.Println(err)
		return
	}
	defer fWrite.Close()

	// 复制
	fRead, err := os.OpenFile(src, os.O_RDONLY, 6)
	if err != nil {
		fmt.Println(err)
		return
	}
	buf := make([]byte, 1024*1024*100)
	for {
		n, err := fRead.Read(buf)
		if err != nil && err == io.EOF {
			//fmt.Println("读取完毕")
			break
		}
		fWrite.Write(buf[:n])
	}
}
func main() {
	var path string = "C:\\Users\\18010\\Pictures\\bg"
	f, err := os.OpenFile(path, os.O_RDONLY, os.ModeDir)
	if err != nil {
		fmt.Println(err)
		return
	}
	defer f.Close()

	// 读取目录
	info, err := f.Readdir(-1)
	if err != nil {
		fmt.Println(err)
		return
	}
	// 遍历返回的切片
	for _, fileInfo := range info {
		if !fileInfo.IsDir() {
			// 判断后缀
			if strings.HasSuffix(fileInfo.Name(), ".jpg") {
				copyImg("C:\\Users\\18010\\Pictures\\bg\\"+fileInfo.Name(), "./"+fileInfo.Name())
			}
		}
	}
}
